package com.settlement.system.controller;

import com.settlement.system.common.constant.Number;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.common.util.common.FasUtil;
import com.settlement.system.handler.TblNcVurcherSummaryCommentWriteHandler;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.TblNcVurcherSummary;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblNcVurcherSummaryService;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * NC凭证明细 前端控制器
 *
 * @author Ximi
 * @since 2020-06-18
 */

@Api(tags = "统一报表-NC凭证明细汇总", description = "统一报表-NC凭证明细汇总")
@RestController
@RequestMapping("/nc/vurcher-summary")
public class TblNcVurcherSummaryController extends ExcelController<TblNcVurcherSummary> {

    @Resource
    private ITblNcVurcherSummaryService tblNcVurcherSummaryService;

    @Override
    public IBaseService init() {
        return tblNcVurcherSummaryService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblNcVurcherSummary.class;
    }

    @Override
    public TblNcVurcherSummaryCommentWriteHandler getCommentWriterHandler() {
        return new TblNcVurcherSummaryCommentWriteHandler();
    }

    @Override
    public String getSheetName() {
        return "NC凭证汇总";
    }

    @Override
    public void processModel(BasePager<Map<String, Object>> model) {
        model.setOrderByField(
                "substr(summary,1,instr(SUMMARY,'/')-1) asc," +
                        "substr(SUMMARY,(instr(SUMMARY,'/')+1),decode(instr(SUMMARY,'，'),0,length(SUMMARY)-(instr(SUMMARY,'/')+1),instr(SUMMARY,'，')-(instr(SUMMARY,'/')+1))) asc," +
                        "decode(origin_coin_borrower_amount,0,0,1) desc,decode(target_coin_borrower_amount,0,0,1) desc,decode(origin_coin_loan_amount,0,0,1) desc,decode(target_coin_loan_amount,0,0,1) desc");
        super.processModel(model);
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        result.put("foot", tblNcVurcherSummaryService.getFoot(model));
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        Object orderByField = model.get("orderByField");
        if (orderByField != null && String.valueOf(Number.ONE).equals(orderByField.toString())) {
            model.put("orderByField",
                    "substr(summary,1,instr(SUMMARY,'/')-1) asc," +
                            "substr(SUMMARY,(instr(SUMMARY,'/')+1),decode(instr(SUMMARY,'，'),0,length(SUMMARY)-(instr(SUMMARY,'/')+1),instr(SUMMARY,'，')-(instr(SUMMARY,'/')+1))) asc," +
                            "decode(origin_coin_borrower_amount,0,0,1) desc,decode(target_coin_borrower_amount,0,0,1) desc,decode(origin_coin_loan_amount,0,0,1) desc,decode(target_coin_loan_amount,0,0,1) desc");
        }
        if (orderByField != null && String.valueOf(Number.TWO).equals(orderByField.toString())) {
            model.put("orderByField",
                    "substr(SUMMARY,(instr(SUMMARY,'/')+1),decode(instr(SUMMARY,'，'),0,length(SUMMARY)-(instr(SUMMARY,'/')+1),instr(SUMMARY,'，')-(instr(SUMMARY,'/')+1))) asc," +
                            "substr(summary,1,instr(SUMMARY,'/')-1) asc," +
                            "decode(origin_coin_borrower_amount,0,0,1) desc,decode(target_coin_borrower_amount,0,0,1) desc,decode(origin_coin_loan_amount,0,0,1) desc,decode(target_coin_loan_amount,0,0,1) desc");
        }
        FasUtil.formatInQueryCondition(model, "companyNo", "editorName");
        super.setParamsModel(model);
    }

    @Override
    protected List<TblNcVurcherSummary> getExportDate(Map<String, Object> params) throws ServiceException {
        setParamsModel(params);
        Object vn = params.get("vurcherNumber");
        Long startVurcherNumber = null;
        if (vn != null && !"".equals(vn.toString())) {
            startVurcherNumber = Long.parseLong(vn.toString());
        }
        List<TblNcVurcherSummary> list = super.getExportDate(params);
        if (startVurcherNumber != null) {
            String preSummaryIn = null;
            Long vurchurNumber = startVurcherNumber;
            for (TblNcVurcherSummary summary : list) {
                String summaryIn = summary.getSummary();

                {
                    if (summaryIn == null) {
                        summaryIn = "";
                    }
                    if (StringUtils.isNotBlank(summaryIn)) {
                        int start = 0;
                        int end = summaryIn.indexOf("，");
                        if (end == -1) {
                            end = summaryIn.length();
                        }
                        summaryIn = summaryIn.substring(start, end);
                    }
                }

                if (!summaryIn.equals(preSummaryIn)) {
                    if (preSummaryIn != null) {
                        vurchurNumber++;
                    }
                    preSummaryIn = summaryIn;
                }

                summary.setVurcherNumber(vurchurNumber + "");
            }
        }
        return list;
    }
}

